﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _PLATFORM.DATA;

namespace _PLATFORM.WEB.Controllers
{ 
    public class GroupController : Controller
    {
        private eCommPlatformRepoEntities db = new eCommPlatformRepoEntities();
        private Guid USER_ID = DATA.Custom_Classes.User.UserID;
        //
        // GET: /Group/

        public ViewResult Index()
        {
            var groups = db.GROUPs.Where(g => g.USER_ID == USER_ID);
            return View(groups.ToList());
        }

        //
        // GET: /Group/Details/5

        public ViewResult Details(decimal id)
        {
            GROUP group = db.GROUPs.Find(id);
            return View(group);
        }

        //
        // GET: /Group/Create

        public ActionResult Create()
        {
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME");
            return View();
        } 

        //
        // POST: /Group/Create

        [HttpPost]
        public ActionResult Create(GROUP group, HttpPostedFileBase fileBase)
        {
            try
            {
                // Get The Image
                HttpPostedFileBase mFile = Request.Files[0];
                group.IMAGE = USER_ID + "_" + mFile.FileName;

                group.USER_ID = _PLATFORM.DATA.Custom_Classes.User.UserID;
                if (ModelState.IsValid)
                {
                    db.GROUPs.Add(group);

                    db.SaveChanges();

                    // Only Write Image If DB Insert Is OK
                    var imagePath = Server.MapPath("~\\Assets\\CategoryImages\\" + group.IMAGE);
                    mFile.SaveAs(imagePath);

                    return RedirectToAction("Index");
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}", validationErrors.Entry.Entity.GetType().FullName,
                                      validationError.PropertyName, validationError.ErrorMessage);
                    }
                }

            }
            

            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", group.USER_ID);
            return View(group);
        }
        
        //
        // GET: /Group/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            GROUP group = db.GROUPs.Find(id);
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", group.USER_ID);
            return View(group);
        }

        //
        // POST: /Group/Edit/5

        [HttpPost]
        public ActionResult Edit(GROUP group)
        {
            if (ModelState.IsValid)
            {
                db.Entry(group).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", group.USER_ID);
            return View(group);
        }

        //
        // GET: /Group/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            GROUP group = db.GROUPs.Find(id);
            return View(group);
        }

        //
        // POST: /Group/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            GROUP group = db.GROUPs.Find(id);
            db.GROUPs.Remove(group);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}